<template>
  <el-dialog :visible.sync="visible"
             :title="!dataForm.id ? $t('add') : $t('update')"
             :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm"
             :rules="dataRule"
             ref="dataForm"
             label-width="120px"
             v-loading="paramsLoading">
      <!--  @keyup.enter.native="dataFormSubmitHandle()" -->
      <el-form-item prop="paramCode"
                    :label="$t('params.paramCode')"
                    style="margin-bottom: 22px;">
        <el-input v-model="dataForm.paramCode"
                  :placeholder="$t('params.paramCode')"></el-input>
      </el-form-item>
      <el-form-item prop="paramValue"
                    :label="$t('params.paramValue')"
                    style="margin-bottom: 22px;">
        <!-- <el-input v-model="dataForm.paramValue"
                  type="textarea"
                  :rows="8"
                  :placeholder="$t('params.paramValue')"></el-input> -->
        <v-jsoneditor v-model="dataForm.paramValue"
                      :options="jsoneditorSetting.options"
                      :plus="jsoneditorSetting.plus"
                      :height="jsoneditorSetting.height"
                      :indentation="jsoneditorSetting.indentation"
                      :search="jsoneditorSetting.search"
                      :path="jsoneditorSetting.path"
                      :color="jsoneditorSetting.color"
                      @error="onError"></v-jsoneditor>
      </el-form-item>
      <el-form-item prop="remark"
                    :label="$t('params.remark')"
                    style="margin-bottom: 22px;">
        <el-input v-model="dataForm.remark"
                  :placeholder="$t('params.remark')"></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button size="small"
                 @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button size="small"
                 type="primary"
                 @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { debounce } from 'lodash'

export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        paramCode: '',
        paramValue: null,
        remark: ''
      },
      jsoneditorSetting: {
        options: { mode: 'code', modes: ['tree', 'code'] },
        plus: false,
        height: '400px',
        indentation: 2,
        search: true,
        color: 'red',
        path: false
      },
      paramsLoading: false,
      dataList: []
    }
  },
  computed: {
    dataRule () {
      return {
        paramCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        paramValue: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.dataForm.paramValue = {}
      this.$nextTick(() => {
        if (this.$refs['dataForm']) this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    getInfo () {
      this.paramsLoading = true
      this.$interface.system.getSystemParamsInfoApi(this.dataForm.id)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data
          }
          this.dataForm.paramValue = JSON.parse(res.data.paramValue)
          this.paramsLoading = false
        }).catch(() => { })
    },
    dataFormSubmitHandle: debounce(function () {
      this.paramsLoading = true
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          this.paramsLoading = false
          return false
        }
        let isSave = false
        if (this.dataForm.paramCode === 'in.line.knowledge.base') {
          isSave = true
        }
        this.dataForm.paramValue = JSON.stringify(this.dataForm.paramValue)
        this.dataList = JSON.parse(this.dataForm.paramValue)
        const changeSysParams = () => {
          if (!this.dataForm.id) {
            return this.$interface.system.postSysParams(this.dataForm)
          } else {
            return this.$interface.system.putSysParams(this.dataForm)
          }
        }
        changeSysParams().then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          if (isSave) {
            window.SITE_CONFIG['wikiInfo'] = this.dataList
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.paramsLoading = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { this.paramsLoading = false })
      })
    }, 1000, { 'leading': true, 'trailing': false }),
    onError () { }
  }
}
</script>
